/**
 * elancom Corporation copy right 2012 - 2015  
 *   
 * $Header$
 * $Id$
 * $Author$
 * $Date$
 * $Revision$
 */
package com.elancom.framework.web;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.PageContext;

/**
 * attach
 * 
 * @author elancom
 * @date 2012-7-26
 */
public class ServletAttribute {
	private PageContext pageContext;

	private ServletRequest request;

	private HttpSession session;

	/**
	 * @param pageContext
	 */
	public ServletAttribute(PageContext pageContext) {
		super();
		this.pageContext = pageContext;
	}

	/**
	 * @param pageContext
	 * @param request
	 */
	public ServletAttribute(PageContext pageContext, ServletRequest request) {
		super();
		this.pageContext = pageContext;
		this.request = request;
	}

	/**
	 * @param pageContext
	 * @param request
	 * @param session
	 */
	public ServletAttribute(PageContext pageContext, ServletRequest request, HttpSession session) {
		super();
		this.pageContext = pageContext;
		this.request = request;
		this.session = session;
	}

	public ServletAttribute effect(PageContext pageContext) {
		this.pageContext = pageContext;
		return this;
	}

	public ServletAttribute effect(ServletRequest request) {
		this.request = request;
		return this;
	}

	public ServletAttribute effect(HttpSession session) {
		this.session = session;
		return this;
	}

	/**
	 * 
	 */
	public ServletAttribute() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param name
	 * @param value
	 */
	public void setAttribute(String name, Object value) {
		if (pageContext != null) {
			pageContext.setAttribute(name, value);
		}

		if (request != null) {
			request.setAttribute(name, value);
		}

		if (session != null) {
			session.setAttribute(name, value);
		}
	}

	/**
	 * @param name
	 */
	public void removeAttribute(String name) {
		if (pageContext != null) {
			pageContext.removeAttribute(name);
		}

		if (request != null) {
			request.removeAttribute(name);
		}

		if (session != null) {
			session.removeAttribute(name);
		}
	}

	/**
	 * @param name
	 * @return
	 */
	public Object getAttribute(String name) {
		Object value = null;

		if (pageContext != null) {
			value = pageContext.getAttribute(name);
		}

		if (request == null && request != null) {
			value = request.getAttribute(name);
		}

		if (value != null && session != null) {
			value = session.getAttribute(name);
		}

		return value;
	}

	/**
	 * @return the pageContext
	 */
	public PageContext getPageContext() {
		return pageContext;
	}

	/**
	 * @return the request
	 */
	public ServletRequest getRequest() {
		return request;
	}

	/**
	 * @return the session
	 */
	public HttpSession getSession() {
		return session;
	}

}
